from contextlib import asynccontextmanager

from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware

from controller.backtestController import backtestRouter
from controller.tradeController import tradeRouter
from db.session import init_sqldb
import uvicorn
from pojo.response import Response
from controller.dataController import dataRouter
from controller.strategyController import strategyRouter

@asynccontextmanager
async def lifespan(app: FastAPI):
    await init_sqldb()
    yield
app = FastAPI(lifespan=lifespan)

# 允许跨域的配置
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.include_router(dataRouter,prefix = '/data', tags=['data'])
app.include_router(strategyRouter,prefix='/strategy', tags=['strategy'])
app.include_router(tradeRouter, prefix='/trade', tags=['trade'])
app.include_router(backtestRouter, prefix='/backtest', tags=['backtest'])

@app.get("/healthz")
def get_health():
    """
    检查健康状态
    :return:
    """
    return Response(
        code = 1,
        message = "status: OK",
    )

